package yxy.game.pm2.command.shop;

import yxy.cherry.data.DataCenter;
import yxy.cherry.data.bean.itemshop;
import yxy.cherry.game.base.message.MessageEmitterHandler;
import yxy.cherry.game.base.message.annotation.MessageSource;
import yxy.cherry.game.base.message.annotation.MessageType;
import yxy.game.pm2.Result;
import yxy.game.pm2.command.Command;
import yxy.game.pm2.message.ShopMessage;
import yxy.game.pm2.module.player.FunctionHandle;
import yxy.game.pm2.module.shop.handle.ItemShopHandle;

import java.util.*;

/**
 * 商店 道具商店
 */
@MessageType(proto = Command.ItemShop, classtype = ShopMessage.ItemShopRequest.class)
public class CMD_ItemShop extends Command<ShopMessage.ItemShopRequest> {

    @Override
    public void execute(MessageSource messageSource, ShopMessage.ItemShopRequest request) {
        try {
            String playerCode = messageSource.getTag();

            ShopMessage.ItemShopResponse.Builder response = ShopMessage.ItemShopResponse.newBuilder();

            //根据开服时间和showtime过滤商品是否展示
            Collection<ShopMessage.Goods> goods = checkShopItemIsShow(playerCode);

            response.addAllGoods(goods);

            send(messageSource, response, Result.Success);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private void send(MessageSource messageSource, ShopMessage.ItemShopResponse.Builder response, Result result) {
        response.setResult(result.packResult());
        MessageEmitterHandler.getInstance().send(messageSource, response.build());
    }

    /**
     * 根据开服时间检查是否展示商品
     *
     * @param playerCode
     */
    private Collection<ShopMessage.Goods> checkShopItemIsShow(String playerCode) {
        Collection<ShopMessage.Goods> goods = new ItemShopHandle().goods(playerCode);

        try {
            //商品表信息
            Map<String, itemshop> datas = DataCenter.getAllData(itemshop.class);
            ArrayList<String> idList = new ArrayList<>();
            //个人的商品信息
            //根据区id获取开服信息,计算已开服日期
            Long alreadlyPassTime = FunctionHandle.getAlreadlyPassTime(playerCode);
            //获取不展示的商品id集合
            Set<Map.Entry<String, itemshop>> entries = datas.entrySet();
            for (Map.Entry<String, itemshop> entry : entries) {
                int showtime = entry.getValue().getShowtime();
                //showtime不等于0且  已开服时间大于showtime才展示商品
                if (alreadlyPassTime - showtime < 0 || showtime == -1) {
                    idList.add(entry.getKey());
                }
            }


            //移除对应的不展示商品
            Iterator<ShopMessage.Goods> iterator = goods.iterator();
            while (iterator.hasNext()) {
                ShopMessage.Goods good = iterator.next();
                int id = good.getId();
                if (idList.contains(String.valueOf(id))) {
                    iterator.remove();
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

        return goods;
    }


}